<?php
/**
 * 一些重要的脚本
 *
 * @author: jim
 * @date: 2017/11/21
 */

namespace app\api\controller;
use think\Db;

class Script extends Common
{
    protected $db_app;
    public function __construct() //Db::connect('database_app');
    {
        parent::__construct();
        $this->db_app = Db::connect('database_app');
    }

    /**
     * 转移用户数据
     */
    public function transferCustomerData() {
//        ini_set("max_execution_time", "120");

        set_time_limit(0);

        // 查询所有用户
        $userList = $this->db_app->table("customer")->field('Id')->select();

        foreach ($userList as $k => $v) {
            $tmpAvatar   = '';
            $tmpRongId   = '';
            $tmpRongToken   = '';


            // 查询属性数据
            $gList = $this->db_app->table("genericattribute")
                ->where('EntityId',$v['Id'])
                ->select();
            foreach ($gList as $gk => $gv) {
                if ($gv['Key'] == 'Avatar') {
                    $tmpAvatar = $gv['Value'];
                }

                if ($gv['Key'] == 'RongCustomerId') {
                    $tmpRongId = $gv['Value'];
                }

                if ($gv['Key'] == 'RongRoken') {
                    $tmpRongToken = $gv['Value'];
                }
            }

            $this->db_app->table('customer')
                ->where('Id',$v['Id'])
                ->update(['avatar'=>$tmpAvatar,'rongCustomerId'=>$tmpRongId,'rongToken'=>$tmpRongToken]);
        }

        $this->outData['code'] = 1;
        $this->outData['message'] = "操作成功";
        return $this->outData;
    }

}